package me.monster.smarttraffic.ui.parking;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.AbstractPresenterActivity;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-12 20:31
 */
public class ParkingActivity extends AbstractPresenterActivity<ParkingPresenter> implements ParkingContract.View {

    @BindView(R.id.btn_parking_query)
    Button btnQuery;
    @BindView(R.id.tv_parking_price)
    TextView tvParkingPrice;
    @BindView(R.id.et_parking)
    EditText etParking;
    @BindView(R.id.btn_parking_query_state)
    Button btnState;
    @BindView(R.id.cb_parking_1)
    CheckBox cbParking1;
    @BindView(R.id.cb_parking_2)
    CheckBox cbParking2;
    @BindView(R.id.pb_parking)
    ProgressBar pbLoading;

    public static void start(Activity context) {
        Intent starter = new Intent(context, ParkingActivity.class);
        context.startActivity(starter);
        context.overridePendingTransition(R.anim.rotate_in, R.anim.alpha_out);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.fetchParkingState();
        mPresenter.fetchParkingPrice();
    }

    @Override
    public ParkingPresenter createPresenter() {
        return new ParkingPresenter();
    }

    @Override
    public View closeToolView() {
        return findViewById(R.id.parking_price_title);
    }

    @Override
    public String getInitTitle() {
        return getString(R.string.dash_menu_p);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_parking;
    }

    @Override
    public void showParkingInfo(Map<String, Boolean> parkingInfo) {
        Boolean parking1 = parkingInfo.get("parking1");
        cbParking1.setChecked(parking1 == null ? false : parking1);

        Boolean parking2 = parkingInfo.get("parking2");
        cbParking2.setChecked(parking2 == null ? false : parking2);

    }

    @Override
    public void showParkingPrice(String price) {
        tvParkingPrice.setText(getString(R.string.parking_price, price));
    }

    @Override
    public void showError(int error) {
        switch (error) {
            case ParkingPresenter.ERROR_NO_INPUT:
                toast("请输入内容");
                break;
            case ParkingPresenter.ERROR_PARKING_PRICE:
                toast("获取费率出错");
                break;
            case ParkingPresenter.ERROR_PARKING_PRICE_SET:
                toast("设置费率出错");
            case ParkingPresenter.ERROR_PARKING_STATE:
                toast("获取停车位状态出错");
                break;
            default:
        }
    }

    @Override
    public void setPriceResult(boolean result) {
        toast(result ? "设置成功" : "设置失败");
    }

    @Override
    public void showLoading() {
        pbLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissLoading() {
        pbLoading.setVisibility(View.GONE);
    }

    @OnClick(R.id.btn_parking_query_state)
    public void parkingState() {
        mPresenter.fetchParkingState();
    }

    @OnClick(R.id.btn_parking_query)
    public void parkingPriceQuery() {
        mPresenter.fetchParkingPrice();
    }

    @OnClick(R.id.btn_parking_set)
    public void setPrice() {
        mPresenter.setParkingPrice(etParking.getText().toString().trim());
    }
}
